package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.Color;
import com.ruoyi.common.core.domain.Quality;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.QualityMapper;
import com.ruoyi.system.service.IQualityService;

/**
 * 公司性质表Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-05
 */
@Service
public class QualityServiceImpl implements IQualityService 
{
    @Autowired
    private QualityMapper qualityMapper;

    /**
     * 查询公司性质表
     * 
     * @param id 公司性质表主键
     * @return 公司性质表
     */
    @Override
    public Quality selectQualityById(Long id)
    {
        return qualityMapper.selectQualityById(id);
    }

    /**
     * 查询公司性质表列表
     * 
     * @param quality 公司性质表
     * @return 公司性质表
     */
    @Override
    @DataScope(QualityAlias = "q")
    public List<Quality> selectQualityList(Quality quality)
    {
        return qualityMapper.selectQualityList(quality);
    }

    @Override
    public List<TreeSelect> selectQualityTree(Quality quality) {
        List<Quality> qualityList = SpringUtils.getAopProxy(this).selectQualityList(quality);
        return buildQualityTreeSelect(qualityList);
    }

    @Override
    public List<Quality> buildQualityTree(List<Quality> qualityList) {
        List<Quality> returnList = new ArrayList<>();
        List<Long> collect = qualityList.stream().map(Quality::getId).collect(Collectors.toList());
        for (Quality quality: qualityList){
            //如果是顶节点，遍历该父节点下的所以子节的
            if(!collect.contains(quality.getParentId())){
                recursionFn(qualityList,quality);
                returnList.add(quality);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = qualityList;
        }
        return returnList;
    }

    @Override
    public List<TreeSelect> buildQualityTreeSelect(List<Quality> qualityList) {
        List<Quality> qualityTree = buildQualityTree(qualityList);
        return qualityTree.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    private void recursionFn(List<Quality> qualityList , Quality quality){
        // 得到子节点列表
        List<Quality> childList = getChildList(qualityList,quality);
        quality.setChildrenQuality(childList);
        for (Quality newQuality : childList)
        {
            if (hasChild(childList, newQuality))
            {
                recursionFn(qualityList, newQuality);
            }
        }
    }

    private List<Quality> getChildList(List<Quality> qualityList, Quality quality){
        List<Quality> returnList = new ArrayList<>();
        Iterator<Quality> iterator = qualityList.iterator();
        while (iterator.hasNext()){
            Quality n = (Quality) iterator.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == quality.getId().longValue())
            {
                returnList.add(n);
            }
        }
        return returnList;
    }

    private boolean hasChild(List<Quality> qualityList, Quality quality)
    {
        return getChildList(qualityList, quality).size() > 0;
    }
}
